import Vue from 'vue'
import VueRouter from 'vue-router'

// 登录路由
const Login = () => import (/* webpackChunkName: "login" */ './../views/Login.vue')
// 注册路由
const Register = () => import (/* webpackChunkName: "register" */ './../views/Register.vue')
// 首页路由
const Index = () => import (/* webpackChunkName: "index" */ './../views/Index.vue')
// 进入首页时显示的路由
const Home = () => import (/* webpackChunkName: "index" */ '../views/home/Home.vue')
// 二级路由的用户列表
const Users = () => import (/* webpackChunkName: "users" */ '../views/users/Users.vue')
// 二级路由的角色列表
const Roles = () => import (/* webpackChunkName: "rights" */ '../views/rights/Roles.vue')
// 二级路由的权限列表
const Rights = () => import (/* webpackChunkName: "rights" */ '../views/rights/Rights.vue')
// 二级路由的商品列表
const Goods = () => import (/* webpackChunkName: "product" */ '../views/products/Goods.vue')
// 二级路由的分类参数
const Params = () => import (/* webpackChunkName: "product" */ './../views/Params.vue')
// 二级路由的商品分类
const Categories = () => import (/* webpackChunkName: "product" */ '../views/products/Categories.vue')
// 二级路由的订单列表
const Orders = () => import (/* webpackChunkName: "orders" */ './../views/Orders.vue')
// 二级路由的数据报表
const Reports = () => import (/* webpackChunkName: "reports" */ './../views/Reports.vue')
// 二级路由的步骤页
const Goodsadd = () => import (/* webpackChunkName: "product" */ '../views/products/goods-add.vue')

Vue.use(VueRouter)

const routes = [
  {
    path: '/', redirect: '/index'
  },
  {
    path: '/login', component: Login
  },
  {
    path: '/register', component: Register
  },
  {
    path: '/index',
    component: Index,
    children: [
      // 配置子路由
      { path: '', component: Home },
      // 二级路由的用户列表
      { path: '/users', component: Users },
      // 二级路由的角色列表
      { path: '/roles', component: Roles },
      // 二级路由的权限列表
      { path: '/rights', component: Rights },
      // 二级路由的商品列表
      { path: '/goods', component: Goods },
      // 二级路由的分类参数
      { path: '/params', component: Params },
      // 二级路由的商品分类
      { path: '/categories', component: Categories },
      // 二级路由的订单列表
      { path: '/orders', component: Orders },
      // 二级路由的数据报表
      { path: '/reports', component: Reports },
      // 二级路由的步骤页
      { path: '/goods-add', component: Goodsadd }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 配置全局前置守卫
// 登录拦截 to 到哪去 from 从哪来 next 是否放行 next() 放行 next('/login') 拦截到登录
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (to.path === '/login' || token) {
    next()
  } else {
    next('/login')
  }
})

export default router
